package com.easyedu.dean.controller;

import com.easyedu.core.entity.po.BuildingAddress;
import com.easyedu.core.entity.po.ResidenceHalls;
import com.easyedu.core.util.R;
import com.easyedu.core.util.StringUtil;
import com.easyedu.dean.entity.dto.request.ReqChangeBuilding;
import com.easyedu.dean.entity.dto.request.ReqChangeType;
import com.easyedu.dean.service.BuildingService;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mengranliu
 * @date 2022/3/15
 */
@Validated
@RestController
@RequestMapping("/dean")
public class BuildingController {

     private final BuildingService buildingService;

     public BuildingController(BuildingService buildingService) {this.buildingService = buildingService;}

     /**
      * 查询教学楼信息
      * @return 教学楼信息列表
      */
     @GetMapping("/building/academic")
     public R queryAcademicBuilding() {

          List<BuildingAddress> academicBuilding = buildingService.getAcademicBuilding();
          if (academicBuilding != null && !academicBuilding.isEmpty()) {
               return R.ok().data(academicBuilding);
          }
          return R.error().message("未查询到任何教学楼");
     }

     /**
      * 查询宿舍楼信息
      * @return
      */
     @GetMapping("/building/sleep")
     public R querySleepBuilding() {

          List<ResidenceHalls> residenceHallsList = buildingService.getSleepBuilding();
          if (residenceHallsList != null && !residenceHallsList.isEmpty()) {
               return R.ok().data(residenceHallsList);
          }
          return R.error().message("未查询到任何宿舍");
     }

     /**
      * 修改宿舍楼和教学楼
      *
      * @param reqChangeBuilding
      */
     @PostMapping("/building/change")
     public R changeBuilding(@RequestBody ReqChangeBuilding reqChangeBuilding) {
          //参数校验
          switch (reqChangeBuilding.getChangeType()) {
               case Add: {

               }
               case Update: {
                    if (StringUtil.isEmpty(reqChangeBuilding.getBuildName())) {
                         return R.error().message("建筑名不能为空");
                    }
                    if (reqChangeBuilding.getX() == 0) {
                         return R.error().message("建筑经度不能为空");
                    }
                    if (reqChangeBuilding.getY() == 0) {
                         return R.error().message("建筑纬度不能为空");
                    }
               }
               case Delete: {
                    if (!reqChangeBuilding.getChangeType().equals(ReqChangeType.Add)) {
                         if (reqChangeBuilding.getBuildingId() == 0) {
                              return R.error().message("建筑id不能为空");
                         }
                    }
                    break;
               }
               default: {
                    return R.error().message("错误的修改类型");
               }
          }
          buildingService.change(reqChangeBuilding);

          return R.ok().message("操作成功");
     }



}
